import java.util.Scanner;

import javax.swing.JOptionPane;

public class BattleshipGame

{

	private char[][] enemyBoard = new char[11][11];
	private char[][] userBoard = new char[11][11];
	private int[][] shipArray = new int[10][10];
	private Scanner scan = new Scanner(System.in);
	private int rowChoice, colChoice, rowNum, colNum, size;
	private boolean play = true, playerOne = false, selectionValid = false;
	private String row, column, direction;

	public static void main(String[] args) {
		Board b1 = new Board();
		b1.fillBoardElement();
		b1.userTurn();
	}

	public void userTurn() {
		while (play) {
			setUpBoard();
		}
	}

	public void fillBoardElement() {
		fillEnemyXAxis();
		fillEnemyYAxis();
		fillUserXAxis();
		fillUserYAxis();
	}

	public void fillEnemyYAxis() {
		enemyBoard[0][0] = ' ';
		enemyBoard[1][0] = '0';
		enemyBoard[2][0] = '1';
		enemyBoard[3][0] = '2';
		enemyBoard[4][0] = '3';
		enemyBoard[5][0] = '4';
		enemyBoard[6][0] = '5';
		enemyBoard[7][0] = '6';
		enemyBoard[8][0] = '7';
		enemyBoard[9][0] = '8';
		enemyBoard[10][0] = '9';
	}

	public void fillUserYAxis() {
		userBoard[1][0] = '0';
		userBoard[2][0] = '1';
		userBoard[3][0] = '2';
		userBoard[4][0] = '3';
		userBoard[5][0] = '4';
		userBoard[6][0] = '5';
		userBoard[7][0] = '6';
		userBoard[8][0] = '7';
		userBoard[9][0] = '8';
		userBoard[10][0] = '9';
	}

	public void fillUserXAxis() {
		userBoard[0][0] = ' ';
		userBoard[0][1] = '0';
		userBoard[0][2] = '1';
		userBoard[0][3] = '2';
		userBoard[0][4] = '3';
		userBoard[0][5] = '4';
		userBoard[0][6] = '5';
		userBoard[0][7] = '6';
		userBoard[0][8] = '7';
		userBoard[0][9] = '8';
		userBoard[0][10] = '9';
	}

	public void fillEnemyXAxis() {
		enemyBoard[0][1] = '0';
		enemyBoard[0][2] = '1';
		enemyBoard[0][3] = '2';
		enemyBoard[0][4] = '3';
		enemyBoard[0][5] = '4';
		enemyBoard[0][6] = '5';
		enemyBoard[0][7] = '6';
		enemyBoard[0][8] = '7';
		enemyBoard[0][9] = '8';
		enemyBoard[0][10] = '9';
	}

	public void fillArray(int row, int column) {
		if (shipArray[row][column] == 's') {
			enemyBoard[row][column] = 'H';
		} else
			enemyBoard[row][column] = 'M';
	}

	public void displayArray() {
		enemyGrid();
		System.out.print("\n\n");
		userGrid();
	}

	public void userGrid() {
		System.out.println("---------User--------");
		for (int c = 0; c < userBoard.length; c++) {
			System.out.print(userBoard[0][c] + " ");
		}
		System.out.print("\n");
		for (int c = 0; c < userBoard.length; c++) {
			System.out.print(userBoard[1][c] + " ");
		}
		System.out.print("\n");
		for (int c = 0; c < userBoard.length; c++) {
			System.out.print(userBoard[2][c] + " ");
		}
		System.out.print("\n");
		for (int c = 0; c < userBoard.length; c++) {
			System.out.print(userBoard[3][c] + " ");
		}
		System.out.print("\n");
		for (int c = 0; c < userBoard.length; c++) {
			System.out.print(userBoard[4][c] + " ");
		}
		System.out.print("\n");
		for (int c = 0; c < userBoard.length; c++) {
			System.out.print(userBoard[5][c] + " ");
		}
		System.out.print("\n");
		for (int c = 0; c < userBoard.length; c++) {
			System.out.print(userBoard[6][c] + " ");
		}
		System.out.print("\n");
		for (int c = 0; c < userBoard.length; c++) {
			System.out.print(userBoard[7][c] + " ");
		}
		System.out.print("\n");
		for (int c = 0; c < userBoard.length; c++) {
			System.out.print(userBoard[8][c] + " ");
		}
		System.out.print("\n");
		for (int c = 0; c < userBoard.length; c++) {
			System.out.print(userBoard[9][c] + " ");
		}
		System.out.print("\n");
		for (int c = 0; c < userBoard.length; c++) {
			System.out.print(userBoard[10][c] + " ");
		}
		System.out.println("\n\n");
	}

	public void enemyGrid() {
		System.out.println("--------Enemy--------");
		for (int c = 0; c < enemyBoard.length; c++) {
			System.out.print(enemyBoard[0][c] + " ");
		}
		System.out.print("\n");
		for (int c = 0; c < enemyBoard.length; c++) {
			System.out.print(enemyBoard[1][c] + " ");
		}
		System.out.print("\n");
		for (int c = 0; c < enemyBoard.length; c++) {
			System.out.print(enemyBoard[2][c] + " ");
		}
		System.out.print("\n");
		for (int c = 0; c < enemyBoard.length; c++) {
			System.out.print(enemyBoard[3][c] + " ");
		}
		System.out.print("\n");
		for (int c = 0; c < enemyBoard.length; c++) {
			System.out.print(enemyBoard[4][c] + " ");
		}
		System.out.print("\n");
		for (int c = 0; c < enemyBoard.length; c++) {
			System.out.print(enemyBoard[5][c] + " ");
		}
		System.out.print("\n");
		for (int c = 0; c < enemyBoard.length; c++) {
			System.out.print(enemyBoard[6][c] + " ");
		}
		System.out.print("\n");
		for (int c = 0; c < enemyBoard.length; c++) {
			System.out.print(enemyBoard[7][c] + " ");
		}
		System.out.print("\n");
		for (int c = 0; c < enemyBoard.length; c++) {
			System.out.print(enemyBoard[8][c] + " ");
		}
		System.out.print("\n");
		for (int c = 0; c < enemyBoard.length; c++) {
			System.out.print(enemyBoard[9][c] + " ");
		}
		System.out.print("\n");
		for (int c = 0; c < enemyBoard.length; c++) {
			System.out.print(enemyBoard[10][c] + " ");
		}
	}

	public void checkArrayForElements(int row, int column) {
		if (playerOne) {
			if (enemyBoard[row][column] == 'h'
					|| enemyBoard[row][column] == 'm') {
				System.out.println("You already nuked that zone");
				setUpBoard();
			} else {
				fillArray(row, column);
				displayArray();
			}
		}
	}

	public void setUpBoard() {
		placeShips();

		// System.out
		// .println("Player one, where would you like to place your mark?\nEnter the row number (0-9)");
		// try {
		// rowChoice = scan.nextInt();
		// if(rowChoice >= 10 || rowChoice <= 0){
		// System.out.println("Invalid choice, try again.");
		// playerOneTurn();
		// }
		// } catch (Exception ex) {
		// System.out.println("Invalid choice, try again.");
		// playerOneTurn();
		// }
		// System.out.println("Enter the column numbers (0-9)");
		// try {
		// colChoice = scan.nextInt();
		// if(colChoice >= 10 || colChoice <= 0){
		// System.out.println("Invalid choice, try again.");
		// playerOneTurn();
		// }
		// } catch (Exception ex) {
		// System.out.println("Invalid choice, try again.");
		// playerOneTurn();
		// }
		// checkArrayForElements(rowChoice, colChoice);
		play = false;
	}

	public void placeShips() {
		// get location and direction of ship
		createShip("carrier", 5); // carrier
		if (selectionValid) {
			createShip("battleship", 4);// battleship
		}
		if (selectionValid) {
			createShip("crusier", 3);// cruiser
		}
		if (selectionValid) {
			createShip("submarine", 3);// submarine
		}
		if (selectionValid) {
			createShip("patrolboat", 2);// patrol boat
		}
	}

	public void checkNorth() {
		if (direction.equals("north")) {
			if (rowNum - size < 1) {
				System.out.println("Invalid selection, try again.");
				selectionValid = false;
				resetArray();
				placeShips();
			} 
			
			else {
				for (int s = rowNum, counter = 0; counter < size; s--, counter++) {
					if(userBoard[s][colNum] == 's'){
						System.out.println("Overlapping ships, try again");
						selectionValid = false;
						resetArray();
						placeShips();
					}
					else{
						userBoard[s][colNum] = 's';
					}
				}
				selectionValid = true;
			}
		}
	}

	public void checkSouth() {
		if (direction.equals("south")) {
			if (rowNum - size > 10) {
				System.out.println("Invalid selection, try again.");
				selectionValid = false;
				resetArray();
				placeShips();
			} 
			
			else {
				for (int s = rowNum, counter = 0; counter < size; s++, counter++) {
					if(userBoard[s][colNum] == 's'){
						System.out.println("Overlapping ships, try again");
						selectionValid = false;
						resetArray();
						placeShips();
					}
					else{
						userBoard[s][colNum] = 's';
					}
				}
				selectionValid = true;
			}
		}
	}

	public void checkEast() {
		if (direction.equals("east")) {
			if (colNum > size) {
				System.out.println("Invalid selection, try again.");
				selectionValid = false;
				resetArray();
				placeShips();
			} 
			
			else {
				for (int s = colNum, counter = 0; counter < size; s++, counter++) {
					if(userBoard[rowNum][s] == 's'){
						System.out.println("Overlapping ships, try again");
						selectionValid = false;
						resetArray();
						placeShips();
					}
					else{
						userBoard[rowNum][s] = 's';
					}
				}
				selectionValid = true;
			}
		}
	}

	public void checkWest() {
		if (direction.equals("west")) {
			if (colNum < size) {
				System.out.println("Invalid selection, try again.");
				selectionValid = false;
				resetArray();
				placeShips();
			} 
			
			else {
				for (int s = colNum, counter = 0; counter < size; s--, counter++) {
					if(userBoard[rowNum][s] == 's'){
						System.out.println("Overlapping ships, try again");
						selectionValid = false;
						resetArray();
						placeShips();
					}
					else{
						userBoard[rowNum][s] = 's';
					}
				}
				selectionValid = true;
			}
		}
	}
	
	public void resetArray(){
		for(int i = 1; i < userBoard.length; i++){
			for(int x = 1; x < userBoard.length; x++){
				userBoard[i][x] = '\0';
			}
		}
	}

	public void createShip(String shipType, int size) {
		
		displayArray();
		
		String shipName = shipType;
		this.size = size;
		
		row = JOptionPane.showInputDialog(null, "What row do you want to put your " + shipName + "?", "0-9", JOptionPane.QUESTION_MESSAGE);
		
		rowNum = Integer.parseInt(row) + 1;
		
		if (rowNum < 0 || rowNum > 10) {
			System.out.println("Invalid selection, try again.");
			selectionValid = false;
			placeShips();
		} 
		
		else {
			selectionValid = true;
		}
		
		column = JOptionPane.showInputDialog(null, "What column do you want to put your " + shipName + "?", "0-9", JOptionPane.QUESTION_MESSAGE);
		
		colNum = Integer.parseInt(column) + 1;
		
		if (colNum < 0 || colNum > 10) {
			System.out.println("Invalid selection, try again.");
			selectionValid = false;
			placeShips();
		} 
		
		else {
			selectionValid = true;
		}
		
		direction = JOptionPane.showInputDialog(null, "What direction do you want to put your " + shipName + "?", "North, South, East, West", JOptionPane.QUESTION_MESSAGE);
		
		direction.toLowerCase();
		
		checkNorth();
		checkSouth();
		checkEast();
		checkWest();
		
		// send shipname, direction, size, and shiplocation to server.
	}
}
